package com.hpjpw.ido.controller;

import java.util.List;

import com.hpjpw.ido.domain.Topic;
import com.hpjpw.ido.service.ITopicService;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hpjpw.ido.domain.TopicImg;
import com.hpjpw.ido.service.ITopicImgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 主题图片，最多三张Controller
 * 
 * @author lk
 * @date 2021-03-27
 */
//@Api(value ="TopicImgController")
@RestController
@RequestMapping("/ido/topic_img")
public class TopicImgController extends BaseController
{
    @Autowired
    private ITopicImgService topicImgService;
    @Autowired
    private ITopicService topicService;

    /**
     * 查询主题图片，最多三张列表
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:list')")
    @GetMapping("/list")
    @ApiOperation("查询主题图片，最多三张列表")
    public TableDataInfo list(TopicImg topicImg)
    {
        startPage();
        List<TopicImg> list = topicImgService.selectTopicImgList(topicImg);
        return getDataTable(list);
    }

    /**
     * 导出主题图片，最多三张列表
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:export')")
    @Log(title = "主题图片，最多三张", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出主题图片，最多三张列表")
    public AjaxResult export(TopicImg topicImg)
    {
        List<TopicImg> list = topicImgService.selectTopicImgList(topicImg);
        ExcelUtil<TopicImg> util = new ExcelUtil<TopicImg>(TopicImg.class);
        return util.exportExcel(list, "topic_img");
    }

    /**
     * 获取主题图片，最多三张详细信息
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取主题图片，最多三张详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(topicImgService.selectTopicImgById(id));
    }

    /**
     * 新增主题图片，最多三张
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:add')")
    @Log(title = "主题图片，最多三张", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增主题图片，最多三张")
    public AjaxResult add(@RequestBody TopicImg topicImg)
    {
        Topic topic = topicService.selectTopicById(topicImg.getTopicId());
        /* 安全校验，不存在此主题 */
        if (StringUtils.isNull(topic)){
            return AjaxResult.error("此主题不存在");
        }
        topicImg.setTopicContent(topic.getContent());
        return toAjax(topicImgService.insertTopicImg(topicImg));
    }

    /**
     * 修改主题图片，最多三张
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:edit')")
    @Log(title = "主题图片，最多三张", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改主题图片，最多三张")
    public AjaxResult edit(@RequestBody TopicImg topicImg)
    {
        return toAjax(topicImgService.updateTopicImg(topicImg));
    }

    /**
     * 删除主题图片，最多三张
     */
    @PreAuthorize("@ss.hasPermi('ido:topic_img:remove')")
    @Log(title = "主题图片，最多三张", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除主题图片，最多三张")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(topicImgService.deleteTopicImgByIds(ids));
    }
}
